/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package comunicacao;

import entities.Resposta;
import entities.Server;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.logging.Level;
import java.util.logging.Logger;
import main.ConfigGeral;

/**
 *
 * @author Leonardo Lino Nº45340
 * @author Diogo Pires Nº40351
 */
public class RmiClientApp {

    RmiAppInterface rmiServer;
    Registry registry;

    ReceiveMessageInterface rmiDatabase;
    Registry registryDatabase;
    String serverAddress = ConfigGeral.CG_SERVER_APP_IP;
    int serverPort = ConfigGeral.CG_SERVER_APP_PORT;
    String serverName = ConfigGeral.CG_SERVER_APP_NAME;

    public Resposta listaTeatros() {

        Resposta resposta = null;

        if (!parteComum()) {
            return null;
        }

        try {
            resposta = rmiServer.listTheaters();
        } catch (RemoteException ex) {
            Logger.getLogger(RmiClientApp.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

        return resposta;
    }

    public boolean restart(Server server) {
        serverAddress = server.getIp();
        serverPort = server.getPort();

        try {
//DATABASE
            if (server.getType() == Server.TYPE_DB) {
                serverName = server.getNodeName() + "temp";
                if (!parteComumDatabase()) {
                    return false;
                }
                rmiDatabase.restart();

            } else {
                //APP SERVER
                serverName = ConfigGeral.CG_SERVER_APP_NAME + "temp";
                if (!parteComum()) {
                    return false;
                }
                rmiServer.restart();
            }

        } catch (RemoteException ex) {
            Logger.getLogger(RmiClientApp.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }

        return true;
    }

    public boolean falha(Server server) {
        serverAddress = server.getIp();
        serverPort = server.getPort();

        try {

            if (server.getType() == Server.TYPE_DB) {
                //DATABASE
                serverName = server.getNodeName();
                if (!parteComumDatabase()) {
                    return false;
                }
                rmiDatabase.falha();

            } else {
                //APP SERVER
                if (!parteComum()) {
                    return false;
                }
                rmiServer.falha();
            }

        } catch (RemoteException ex) {
            Logger.getLogger(RmiClientApp.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }

        return true;
    }

    private boolean parteComumDatabase() {

        try {
            registryDatabase = LocateRegistry.getRegistry(
                    serverAddress, serverPort);

            // look up the remote object
            rmiDatabase = (ReceiveMessageInterface) (registryDatabase.lookup(serverName));

        } catch (RemoteException e) {
            e.printStackTrace();
            return false;
        } catch (NotBoundException e) {
            e.printStackTrace();
            return false;
        }
        return true;

    }

    private boolean parteComum() {

        try {
            registry = LocateRegistry.getRegistry(
                    serverAddress, serverPort);

            // look up the remote object
            rmiServer = (RmiAppInterface) (registry.lookup(serverName));

        } catch (RemoteException e) {
            e.printStackTrace();
            return false;
        } catch (NotBoundException e) {
            e.printStackTrace();
            return false;
        }
        return true;

    }

}
